package ar.com.angrymachine.hunter.activities;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

import ar.com.angrymachine.hunter.Activity;
import ar.com.angrymachine.hunter.Game;
import ar.com.angrymachine.hunter.GraphicStyles;
import ar.com.angrymachine.hunter.Locale;
import ar.com.angrymachine.hunter.HunterResourceManager;
import ar.com.angrymachine.hunter.gameplay.Monster;
import ar.com.angrymachine.hunter.utils.DungeonGenerationUtils;
import ar.com.angrymachine.utils.DecrementalTimer;
import ar.com.angrymachine.utils.IncrementalTimer;

public class SpawnerActivity extends Activity
{
    private IncrementalTimer spawnTimer;
    private DecrementalTimer healthTimer;

    public Monster monster;
    public SpawnerActivity(int level, int spawnTime)
    {
        spawnTimer = new IncrementalTimer(spawnTime);
        healthTimer = new DecrementalTimer(level*500);
        monster = DungeonGenerationUtils.createMonster(level, null);
    }
    
    public String getDescription()
    {
        return Locale.getSpawnerDescription(this);
    }
        
    public String getActionText()
    {
        if(isCompleted())
        {
            return Locale.DESTROYED;
        }
        else
        {
            if(isRunning())
            {
                return Locale.ATTACKING;
            }
            else
            {
                return Locale.ATTACK;
            }
        }
    }
    
    public boolean isCompleted()
    {
        return healthTimer.isComplete();
    }
    
    public boolean isUpdatedWhenNotVisible()
    {
    	return true;
    }

    public void update(int delta, boolean action, boolean timeIsRunning)
    {
        if(timeIsRunning)
        {
            if(action)
            {
                healthTimer.update((int)Game.current.player.getHitDamage());
                if(healthTimer.isComplete())
                {
                    // die
                	end(true);
                }
            }
            // spawn?
            spawnTimer.update(delta);
            if(spawnTimer.isComplete())
            {
                spawnTimer.reset();
                Monster newMonster = (Monster)monster.clone();
                newMonster.name = Locale.getSpawnedMonsterName(newMonster);
            	Game.current.currentLevel.addActivityInTail(new FightActivity(newMonster));
            }
        }
    }
    
    public void paint(Graphics g, Rectangle activityArea)
    {
   		GraphicStyles.paintDefaultActivity(g, this, activityArea, HunterResourceManager.getImage("altar.png"));
        if(!isCompleted())
        {
        	Rectangle[] bars = GraphicStyles.getBarAreas(activityArea, 2);
            Image healthIcon = HunterResourceManager.getImage("heart-full.png");
            Image attackIcon = HunterResourceManager.getImage("spawn-icon.png");
        	int iconsWidth = Math.max(healthIcon.getWidth(null), attackIcon.getWidth(null));
        	// attack
            g.setColor(Color.YELLOW);
            GraphicStyles.paintIconizedBar(g, attackIcon, iconsWidth, bars[0], spawnTimer.currentTime, spawnTimer.eventTime);
            // health
            g.setColor(Color.RED);
            GraphicStyles.paintIconizedBar(g, healthIcon, iconsWidth, bars[1], healthTimer.currentTime, healthTimer.intialTime);
        }
    }
}
